package com.sk.weichat.ui.me.redpacket;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sk.weichat.R;
import com.sk.weichat.alipay.AliPayHelper;
import com.sk.weichat.alipay.PayResult;
import com.sk.weichat.bean.Friend;
import com.sk.weichat.bean.RoomMember;
import com.sk.weichat.bean.SendRedPacketBean;
import com.sk.weichat.bean.message.ChatMessage;
import com.sk.weichat.bean.message.XmppMessage;
import com.sk.weichat.db.dao.ChatMessageDao;
import com.sk.weichat.db.dao.FriendDao;
import com.sk.weichat.db.dao.RoomMemberDao;
import com.sk.weichat.helper.DialogHelper;
import com.sk.weichat.ui.base.BaseLoginActivity;
import com.sk.weichat.ui.message.ChatActivity;
import com.sk.weichat.util.TimeUtils;
import com.sk.weichat.util.ToastUtil;
import com.sk.weichat.view.MaxTextTwoLengthFilter;
import com.sk.weichat.view.RedPacketDialog;
import com.sk.weichat.xmpp.listener.ChatMessageListener;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * @Author: chenzl
 * @ClassName: MucSendRedPacketActivity1
 * @Description: 群聊发红包
 * @CreateDate: 2019/10/9 17:10
 */
public class MucSendRedPacketActivity1 extends BaseLoginActivity implements View.OnClickListener {

    private ImageView img_left;
    private TextView tvRedpacketTips;
    private TextView tvRedpacketSum;
    private ImageView img_pin;
    private EditText edit_redpacket_sum;
    private TextView tvRedpacketUnit;
    private TextView tv_redpacket_present;
    private TextView tv_redpacket_type;
    private TextView tv_redpacket_number;//红包个数
    private EditText edit_redpacket_number;//填写红包个数
    private TextView tv_redpacket_number_unit;//
    private TextView tv_group_numble;
    private EditText edit_input_blessing;
    private Button btnSendRedpacket;

    private boolean isPinRedPacket = true;//判断是拼手气红包 还是普通红包
    private SendRedPacketBean mSendRedPacketBean;
    private String roomId;
    private String toUserId;
    private String mLoginUserId;
    private String mLoginNickName;
    private MutableLiveData<PayResult> alipayResult = new MutableLiveData<>();

    private Friend mFriend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_muc_send_redpacket);
        getSupportActionBar().hide();
        roomId = getIntent().getStringExtra("roomId");
        toUserId = getIntent().getStringExtra("toUserId");
        mLoginUserId = coreManager.getSelf().getUserId();
        mLoginNickName = coreManager.getSelf().getNickName();
        mFriend = FriendDao.getInstance().getFriend(mLoginUserId, toUserId);// 重新获取friend对象
        initView();
    }

    private void initView() {
        img_left = findViewById(R.id.img_left);
        tvRedpacketTips = findViewById(R.id.tv_redpacket_tips);
        tvRedpacketSum = findViewById(R.id.tv_redpacket_sum);
        img_pin = findViewById(R.id.img_pin);
        edit_redpacket_sum = findViewById(R.id.edit_redpacket_sum);
        tvRedpacketUnit = findViewById(R.id.tv_redpacket_unit);
        tv_redpacket_present = findViewById(R.id.tv_redpacket_present);
        tv_redpacket_type = findViewById(R.id.tv_redpacket_type);
        tv_redpacket_number = findViewById(R.id.tv_redpacket_number);
        edit_redpacket_number = findViewById(R.id.edit_redpacket_number);
        tv_redpacket_number_unit = findViewById(R.id.tv_redpacket_number_unit);
        tv_group_numble = findViewById(R.id.tv_group_numble);
        edit_input_blessing = findViewById(R.id.edit_input_blessing);
        /*获取群成员数量*/
        int RoomCount = RoomMemberDao.getInstance().getRoomMember(roomId).size();
        tv_group_numble.setText("本群共" + RoomCount + "人");
        //显示配置祝福语
        edit_input_blessing.setHint(coreManager.readConfigBean().getDefaultGreetings());
        btnSendRedpacket = findViewById(R.id.btn_send_redpacket);

        img_left.setOnClickListener(this);
        tv_redpacket_type.setOnClickListener(this);
        btnSendRedpacket.setOnClickListener(this);
        edit_redpacket_sum.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                //删除.后面超过两位的数字
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        edit_redpacket_sum.setText(s);
                        edit_redpacket_sum.setSelection(s.length());
                    }
                }

                //如果.在起始位置,则起始位置自动补0
                if (s.toString().trim().equals(".")) {
                    s = "0" + s;
                    edit_redpacket_sum.setText(s);
                    edit_redpacket_sum.setSelection(2);
                }

                //如果起始位置为0并且第二位跟的不是".",则无法后续输入
                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        edit_redpacket_sum.setText(s.subSequence(0, 1));
                        edit_redpacket_sum.setSelection(1);
                        return;
                    }
                }
                //限制0.00输入
//                if (s.toString().startsWith("0")
//                        && s.toString().trim().length() > 3) {
//                    if (s.toString().substring(2,3).equals("0") && s.toString().substring(3,4).equals("0")) {
//                        edit_redpacket_sum.setText(s.subSequence(0,3));
//                        edit_redpacket_sum.setSelection(3);
//                        return;
//                    }
//                }

                if (TextUtils.isEmpty(s.toString())) {
                    if (!TextUtils.isEmpty(edit_redpacket_number.getText())) {
                        int num = Integer.parseInt(edit_redpacket_number.getText().toString());
                        if (num == 0) {
                            tvRedpacketTips.setVisibility(View.VISIBLE);
                            tvRedpacketTips.setText("红包个数不可低于1个");
                            tv_redpacket_number.setTextColor(getResources().getColor(R.color.color_ff2626));
                            tv_redpacket_number_unit.setTextColor(getResources().getColor(R.color.color_ff2626));
                            edit_redpacket_number.setTextColor(getResources().getColor(R.color.color_ff2626));
                        } else if (num > 100) {
                            tvRedpacketTips.setVisibility(View.VISIBLE);
                            tvRedpacketTips.setText("红包个数不可大于100个");
                            tv_redpacket_number.setTextColor(getResources().getColor(R.color.color_ff2626));
                            tv_redpacket_number_unit.setTextColor(getResources().getColor(R.color.color_ff2626));
                            edit_redpacket_number.setTextColor(getResources().getColor(R.color.color_ff2626));
                        } else {
                            tvRedpacketTips.setVisibility(View.GONE);
                            tv_redpacket_number.setTextColor(getResources().getColor(R.color.text_color_main));
                            tv_redpacket_number_unit.setTextColor(getResources().getColor(R.color.text_color_main));
                            edit_redpacket_number.setTextColor(getResources().getColor(R.color.text_color_main));
                        }
                    }
                    btnSendRedpacket.setText("0.00元 塞钱进红包");
                    btnSendRedpacket.setEnabled(false);
                    btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                    btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                    return;
                } else {
                    double sum = Double.parseDouble(s.toString());
                    DecimalFormat decimalFormat = new DecimalFormat("0.00");
                    if (isPinRedPacket) {
                        String distanceString = decimalFormat.format(sum);
                        btnSendRedpacket.setText(distanceString + "元 塞钱进红包");
                        if (sum > 20000) {
                            tvRedpacketTips.setVisibility(View.VISIBLE);
                            tvRedpacketTips.setText("单次支付总额不可超过20000元");
                            tvRedpacketSum.setTextColor(getResources().getColor(R.color.color_ff2626));
                            tvRedpacketUnit.setTextColor(getResources().getColor(R.color.color_ff2626));
                            edit_redpacket_sum.setTextColor(getResources().getColor(R.color.color_ff2626));

                            btnSendRedpacket.setEnabled(false);
                            btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                            btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                            return;
                        } else {
                            if (!TextUtils.isEmpty(edit_redpacket_number.getText())) {
                                int num = Integer.parseInt(edit_redpacket_number.getText().toString());
                                if (num == 0) {
                                    tvRedpacketTips.setVisibility(View.VISIBLE);
                                    tvRedpacketTips.setText("红包个数不可低于1个");
                                    tv_redpacket_number.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    tv_redpacket_number_unit.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    edit_redpacket_number.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    btnSendRedpacket.setEnabled(false);
                                    btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                                    btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                                    return;
                                } else if (num > 100) {
                                    tvRedpacketTips.setVisibility(View.VISIBLE);
                                    tvRedpacketTips.setText("红包个数不可大于100个");
                                    tv_redpacket_number.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    tv_redpacket_number_unit.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    edit_redpacket_number.setTextColor(getResources().getColor(R.color.color_ff2626));

                                    btnSendRedpacket.setEnabled(false);
                                    btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                                    btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                                    return;
                                } else {
                                    if ((sum / num) < 0.01) {
                                        tvRedpacketTips.setVisibility(View.VISIBLE);
                                        tvRedpacketTips.setText("单个红包金额不可低于0.01元");
                                        tvRedpacketSum.setTextColor(getResources().getColor(R.color.color_ff2626));
                                        tvRedpacketUnit.setTextColor(getResources().getColor(R.color.color_ff2626));
                                        edit_redpacket_sum.setTextColor(getResources().getColor(R.color.color_ff2626));

                                        btnSendRedpacket.setEnabled(false);
                                        btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                                        btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                                        return;
                                    } else if ((sum / num) > 200) {
                                        tvRedpacketTips.setVisibility(View.VISIBLE);
                                        tvRedpacketTips.setText("单个红包金额不可超过200元");
                                        tvRedpacketSum.setTextColor(getResources().getColor(R.color.color_ff2626));
                                        tvRedpacketUnit.setTextColor(getResources().getColor(R.color.color_ff2626));
                                        edit_redpacket_sum.setTextColor(getResources().getColor(R.color.color_ff2626));

                                        btnSendRedpacket.setEnabled(false);
                                        btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                                        btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                                        return;
                                    } else {
                                        tvRedpacketTips.setVisibility(View.GONE);
                                        tvRedpacketSum.setTextColor(getResources().getColor(R.color.text_color_main));
                                        tvRedpacketUnit.setTextColor(getResources().getColor(R.color.text_color_main));
                                        edit_redpacket_sum.setTextColor(getResources().getColor(R.color.text_color_main));

                                        btnSendRedpacket.setEnabled(true);
                                        btnSendRedpacket.setTextColor(getResources().getColor(R.color.color_FF4B4B));
                                        btnSendRedpacket.setBackgroundResource(R.drawable.bg_dark_yellow_radius25);
                                        return;
                                    }
                                }
                            } else {
                                tvRedpacketTips.setVisibility(View.GONE);
                                tvRedpacketSum.setTextColor(getResources().getColor(R.color.text_color_main));
                                tvRedpacketUnit.setTextColor(getResources().getColor(R.color.text_color_main));
                                edit_redpacket_sum.setTextColor(getResources().getColor(R.color.text_color_main));

                                btnSendRedpacket.setEnabled(false);
                                btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                                btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                            }
                        }
                    } else {//普通红包
                        if (sum > 200) {
                            tvRedpacketTips.setVisibility(View.VISIBLE);
                            tvRedpacketTips.setText("单个红包金额不可超过200元");
                            tvRedpacketSum.setTextColor(getResources().getColor(R.color.color_ff2626));
                            tvRedpacketUnit.setTextColor(getResources().getColor(R.color.color_ff2626));
                            edit_redpacket_sum.setTextColor(getResources().getColor(R.color.color_ff2626));

                            btnSendRedpacket.setEnabled(false);
                            btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                            btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                            return;
                        } else {
                            if (!TextUtils.isEmpty(edit_redpacket_number.getText())) {
                                int num = Integer.parseInt(edit_redpacket_number.getText().toString());
                                if (num == 0) {
                                    tvRedpacketTips.setVisibility(View.VISIBLE);
                                    tvRedpacketTips.setText("红包个数不可低于1个");
                                    tv_redpacket_number.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    tv_redpacket_number_unit.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    edit_redpacket_number.setTextColor(getResources().getColor(R.color.color_ff2626));

                                    String distanceString = decimalFormat.format(sum);
                                    btnSendRedpacket.setText(distanceString + "元 塞钱进红包");
                                    btnSendRedpacket.setEnabled(false);
                                    btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                                    btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                                    return;
                                } else if (num > 100) {
                                    tvRedpacketTips.setVisibility(View.VISIBLE);
                                    tvRedpacketTips.setText("红包个数不可大于100个");
                                    tv_redpacket_number.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    tv_redpacket_number_unit.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    edit_redpacket_number.setTextColor(getResources().getColor(R.color.color_ff2626));

                                    String distanceString = decimalFormat.format(sum * num);
                                    btnSendRedpacket.setText(distanceString + "元 塞钱进红包");
                                    btnSendRedpacket.setEnabled(false);
                                    btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                                    btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                                    return;
                                } else {
                                    String distanceString = decimalFormat.format(sum * num);
                                    btnSendRedpacket.setText(distanceString + "元 塞钱进红包");
                                    if (sum < 0.01) {
                                        tvRedpacketTips.setVisibility(View.VISIBLE);
                                        tvRedpacketTips.setText("单个红包金额不可低于0.01元");
                                        tvRedpacketSum.setTextColor(getResources().getColor(R.color.color_ff2626));
                                        tvRedpacketUnit.setTextColor(getResources().getColor(R.color.color_ff2626));
                                        edit_redpacket_sum.setTextColor(getResources().getColor(R.color.color_ff2626));

                                        btnSendRedpacket.setEnabled(false);
                                        btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                                        btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                                        return;
                                    } else if (sum > 200) {
                                        tvRedpacketTips.setVisibility(View.VISIBLE);
                                        tvRedpacketTips.setText("单个红包金额不可超过200元");
                                        tvRedpacketSum.setTextColor(getResources().getColor(R.color.color_ff2626));
                                        tvRedpacketUnit.setTextColor(getResources().getColor(R.color.color_ff2626));
                                        edit_redpacket_sum.setTextColor(getResources().getColor(R.color.color_ff2626));

                                        btnSendRedpacket.setEnabled(false);
                                        btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                                        btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                                        return;
                                    } else {
                                        tvRedpacketTips.setVisibility(View.GONE);
                                        tvRedpacketSum.setTextColor(getResources().getColor(R.color.text_color_main));
                                        tvRedpacketUnit.setTextColor(getResources().getColor(R.color.text_color_main));
                                        edit_redpacket_sum.setTextColor(getResources().getColor(R.color.text_color_main));

                                        btnSendRedpacket.setEnabled(true);
                                        btnSendRedpacket.setTextColor(getResources().getColor(R.color.color_FF4B4B));
                                        btnSendRedpacket.setBackgroundResource(R.drawable.bg_dark_yellow_radius25);
                                        return;
                                    }
                                }
                            } else {
                                tvRedpacketTips.setVisibility(View.GONE);
                                tvRedpacketSum.setTextColor(getResources().getColor(R.color.text_color_main));
                                tvRedpacketUnit.setTextColor(getResources().getColor(R.color.text_color_main));
                                edit_redpacket_sum.setTextColor(getResources().getColor(R.color.text_color_main));

                                btnSendRedpacket.setEnabled(false);
                                btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                                btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                            }
                        }
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }

        });

        edit_redpacket_number.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //起始输入0 不给输入
                if (s.toString().startsWith("0")) {
                    if (s.toString().trim().length() > 1) {
                        edit_redpacket_number.setText(s.subSequence(0, 1));
                        edit_redpacket_number.setSelection(1);
                    }
                    tvRedpacketTips.setVisibility(View.VISIBLE);
                    tvRedpacketTips.setText("红包个数不可低于1个");
                    tv_redpacket_number.setTextColor(getResources().getColor(R.color.color_ff2626));
                    tv_redpacket_number_unit.setTextColor(getResources().getColor(R.color.color_ff2626));
                    edit_redpacket_number.setTextColor(getResources().getColor(R.color.color_ff2626));
                    return;
                }

                //红包个数输入为空
                if (TextUtils.isEmpty(s.toString())) {
                    if (!TextUtils.isEmpty(edit_redpacket_sum.getText().toString())) {
                        Double sum = Double.valueOf(edit_redpacket_sum.getText().toString());
                        DecimalFormat decimalFormat = new DecimalFormat("0.00");
                        String distanceString = decimalFormat.format(sum);
                        //构造方法的字符格式这里如果小数不足2位,会以0补足.
                        btnSendRedpacket.setText(distanceString + "元 塞钱进红包");
                        btnSendRedpacket.setEnabled(false);
                        btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                        btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);

                        if (sum < 0.01) {
                            tvRedpacketTips.setVisibility(View.VISIBLE);
                            tvRedpacketTips.setText("单个红包金额不可低于0.01元");
                            tvRedpacketSum.setTextColor(getResources().getColor(R.color.color_ff2626));
                            tvRedpacketUnit.setTextColor(getResources().getColor(R.color.color_ff2626));
                            edit_redpacket_sum.setTextColor(getResources().getColor(R.color.color_ff2626));
                            return;
                        }
                        if (isPinRedPacket) {
                            if (sum > 20000) {
                                tvRedpacketTips.setVisibility(View.VISIBLE);
                                tvRedpacketTips.setText("单次支付总额不可超过20000元");
                                tvRedpacketSum.setTextColor(getResources().getColor(R.color.color_ff2626));
                                tvRedpacketUnit.setTextColor(getResources().getColor(R.color.color_ff2626));
                                edit_redpacket_sum.setTextColor(getResources().getColor(R.color.color_ff2626));
                                return;
                            } else {
                                tvRedpacketTips.setVisibility(View.GONE);
                                tvRedpacketSum.setTextColor(getResources().getColor(R.color.text_color_main));
                                tvRedpacketUnit.setTextColor(getResources().getColor(R.color.text_color_main));
                                edit_redpacket_sum.setTextColor(getResources().getColor(R.color.text_color_main));
                            }
                        } else {
                            if (sum > 200) {
                                tvRedpacketTips.setVisibility(View.VISIBLE);
                                tvRedpacketTips.setText("单个红包金额不可超过200元");
                                tvRedpacketSum.setTextColor(getResources().getColor(R.color.color_ff2626));
                                tvRedpacketUnit.setTextColor(getResources().getColor(R.color.color_ff2626));
                                edit_redpacket_sum.setTextColor(getResources().getColor(R.color.color_ff2626));
                                return;
                            } else {
                                tvRedpacketTips.setVisibility(View.GONE);
                                tvRedpacketSum.setTextColor(getResources().getColor(R.color.text_color_main));
                                tvRedpacketUnit.setTextColor(getResources().getColor(R.color.text_color_main));
                                edit_redpacket_sum.setTextColor(getResources().getColor(R.color.text_color_main));
                            }
                        }
                    } else {
                        tvRedpacketTips.setVisibility(View.GONE);
                        tvRedpacketSum.setTextColor(getResources().getColor(R.color.text_color_main));
                        tvRedpacketUnit.setTextColor(getResources().getColor(R.color.text_color_main));
                        edit_redpacket_sum.setTextColor(getResources().getColor(R.color.text_color_main));

                        tv_redpacket_number.setTextColor(getResources().getColor(R.color.text_color_main));
                        tv_redpacket_number_unit.setTextColor(getResources().getColor(R.color.text_color_main));
                        edit_redpacket_number.setTextColor(getResources().getColor(R.color.text_color_main));

                        btnSendRedpacket.setText("0.00元 塞钱进红包");
                        btnSendRedpacket.setEnabled(false);
                        btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                        btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                    }
                    tv_redpacket_number.setTextColor(getResources().getColor(R.color.text_color_main));
                    tv_redpacket_number_unit.setTextColor(getResources().getColor(R.color.text_color_main));
                    edit_redpacket_number.setTextColor(getResources().getColor(R.color.text_color_main));

                } else {
                    //红包个数
                    int number = Integer.parseInt(s.toString());
                    if (number > 100) {
                        tvRedpacketTips.setVisibility(View.VISIBLE);
                        tvRedpacketTips.setText("红包个数不可大于100个");
                        tv_redpacket_number.setTextColor(getResources().getColor(R.color.color_ff2626));
                        tv_redpacket_number_unit.setTextColor(getResources().getColor(R.color.color_ff2626));
                        edit_redpacket_number.setTextColor(getResources().getColor(R.color.color_ff2626));
                        btnSendRedpacket.setEnabled(false);
                        btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                        btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                    } else {
                        tv_redpacket_number.setTextColor(getResources().getColor(R.color.text_color_main));
                        tv_redpacket_number_unit.setTextColor(getResources().getColor(R.color.text_color_main));
                        edit_redpacket_number.setTextColor(getResources().getColor(R.color.text_color_main));
                        if (!TextUtils.isEmpty(edit_redpacket_sum.getText().toString())) {
                            Double sum = Double.valueOf(edit_redpacket_sum.getText().toString());
                            if (isPinRedPacket) {
                                DecimalFormat decimalFormat = new DecimalFormat("0.00");
                                String distanceString = decimalFormat.format(sum);
                                //构造方法的字符格式这里如果小数不足2位,会以0补足.
                                btnSendRedpacket.setText(distanceString + "元 塞钱进红包");
                                if (sum > 20000) {
                                    tvRedpacketTips.setVisibility(View.VISIBLE);
                                    tvRedpacketTips.setText("单次支付总额不可超过20000元");
                                    tvRedpacketSum.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    tvRedpacketUnit.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    edit_redpacket_sum.setTextColor(getResources().getColor(R.color.color_ff2626));

                                    btnSendRedpacket.setEnabled(false);
                                    btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                                    btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                                    return;
                                } else if ((sum / number) < 0.01) {
                                    tvRedpacketTips.setVisibility(View.VISIBLE);
                                    tvRedpacketTips.setText("单个红包金额不可低于0.01元");
                                    tvRedpacketSum.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    tvRedpacketUnit.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    edit_redpacket_sum.setTextColor(getResources().getColor(R.color.color_ff2626));

                                    btnSendRedpacket.setEnabled(false);
                                    btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                                    btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                                    return;
                                } else if ((sum / number) > 200) {
                                    tvRedpacketTips.setVisibility(View.VISIBLE);
                                    tvRedpacketTips.setText("单个红包金额不可超过200元");
                                    tvRedpacketSum.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    tvRedpacketUnit.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    edit_redpacket_sum.setTextColor(getResources().getColor(R.color.color_ff2626));

                                    btnSendRedpacket.setEnabled(false);
                                    btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                                    btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                                    return;
                                } else {
                                    tvRedpacketTips.setVisibility(View.GONE);
                                    tvRedpacketSum.setTextColor(getResources().getColor(R.color.text_color_main));
                                    tvRedpacketUnit.setTextColor(getResources().getColor(R.color.text_color_main));
                                    edit_redpacket_sum.setTextColor(getResources().getColor(R.color.text_color_main));

                                    tv_redpacket_number.setTextColor(getResources().getColor(R.color.text_color_main));
                                    tv_redpacket_number_unit.setTextColor(getResources().getColor(R.color.text_color_main));
                                    edit_redpacket_number.setTextColor(getResources().getColor(R.color.text_color_main));

                                    btnSendRedpacket.setEnabled(true);
                                    btnSendRedpacket.setTextColor(getResources().getColor(R.color.color_FF4B4B));
                                    btnSendRedpacket.setBackgroundResource(R.drawable.bg_dark_yellow_radius25);
                                }

                            } else {
                                DecimalFormat decimalFormat = new DecimalFormat("0.00");
                                String distanceString = decimalFormat.format(sum * number);
                                //构造方法的字符格式这里如果小数不足2位,会以0补足.
                                btnSendRedpacket.setText(distanceString + "元 塞钱进红包");
                                if (sum < 0.01) {
                                    tvRedpacketTips.setVisibility(View.VISIBLE);
                                    tvRedpacketTips.setText("单个红包金额不可低于0.01元");
                                    tvRedpacketSum.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    tvRedpacketUnit.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    edit_redpacket_sum.setTextColor(getResources().getColor(R.color.color_ff2626));

                                    btnSendRedpacket.setEnabled(false);
                                    btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                                    btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                                    return;
                                } else if (sum > 200) {
                                    tvRedpacketTips.setVisibility(View.VISIBLE);
                                    tvRedpacketTips.setText("单个红包金额不可超过200元");
                                    tvRedpacketSum.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    tvRedpacketUnit.setTextColor(getResources().getColor(R.color.color_ff2626));
                                    edit_redpacket_sum.setTextColor(getResources().getColor(R.color.color_ff2626));

                                    btnSendRedpacket.setEnabled(false);
                                    btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                                    btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                                    return;
                                } else {
                                    tvRedpacketTips.setVisibility(View.GONE);
                                    tvRedpacketSum.setTextColor(getResources().getColor(R.color.text_color_main));
                                    tvRedpacketUnit.setTextColor(getResources().getColor(R.color.text_color_main));
                                    edit_redpacket_sum.setTextColor(getResources().getColor(R.color.text_color_main));

                                    tv_redpacket_number.setTextColor(getResources().getColor(R.color.text_color_main));
                                    tv_redpacket_number_unit.setTextColor(getResources().getColor(R.color.text_color_main));
                                    edit_redpacket_number.setTextColor(getResources().getColor(R.color.text_color_main));

                                    btnSendRedpacket.setEnabled(true);
                                    btnSendRedpacket.setTextColor(getResources().getColor(R.color.color_FF4B4B));
                                    btnSendRedpacket.setBackgroundResource(R.drawable.bg_dark_yellow_radius25);
                                }
                            }
                        } else {
                            tvRedpacketTips.setVisibility(View.GONE);
                            tvRedpacketSum.setTextColor(getResources().getColor(R.color.text_color_main));
                            tvRedpacketUnit.setTextColor(getResources().getColor(R.color.text_color_main));
                            edit_redpacket_sum.setTextColor(getResources().getColor(R.color.text_color_main));

                            tv_redpacket_number.setTextColor(getResources().getColor(R.color.text_color_main));
                            tv_redpacket_number_unit.setTextColor(getResources().getColor(R.color.text_color_main));
                            edit_redpacket_number.setTextColor(getResources().getColor(R.color.text_color_main));

                            btnSendRedpacket.setText("0.00元 塞钱进红包");
                            btnSendRedpacket.setEnabled(false);
                            btnSendRedpacket.setTextColor(getResources().getColor(R.color.white));
                            btnSendRedpacket.setBackgroundResource(R.drawable.bg_gray_radius25);
                        }
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        edit_input_blessing.setFilters(new InputFilter[]{new MaxTextTwoLengthFilter(24)});

        //支付宝支付返回
        alipayResult.observe(this, new Observer<PayResult>() {
            @Override
            public void onChanged(@Nullable PayResult payResult) {
                if (payResult.isResultFlag()) {
                    //支付成功
                    //ToastUtil.showToast(mContext, getString(R.string.pay_success));
                    Intent data = new Intent(MucSendRedPacketActivity1.this, ChatActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("messageId", mSendRedPacketBean.getMessageId());
                    data.putExtras(bundle);
                    setResult(ChatActivity.REQUEST_CODE_SEND_RED_PT, data);
                    finish();
                } else if (payResult.getResultStatus().equals("6001")) {
                    ToastUtil.showToast(mContext, getString(R.string.pay_cancel_failed));
                    //支付失败删除本地 红包消息记录
                    ChatMessageDao.getInstance().deleteSingleChatMessage(mLoginUserId, toUserId, mSendRedPacketBean.getMessageId());
                } else {
                    //支付失败删除本地 红包消息记录
                    ChatMessageDao.getInstance().deleteSingleChatMessage(mLoginUserId, toUserId, mSendRedPacketBean.getMessageId());
                    //失败
                    ToastUtil.showToast(mContext, getString(R.string.pay_failed));
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v == img_left) {
            finish();
        } else if (v == tv_redpacket_type) {
            if (isPinRedPacket) {
                isPinRedPacket = false;
                img_pin.setVisibility(View.GONE);
                tvRedpacketSum.setText("单个金额");
                tv_redpacket_present.setText("当前为普通红包，改为");
                tv_redpacket_type.setText("拼手气红包");

                if (!TextUtils.isEmpty(edit_redpacket_sum.getText().toString()) && !TextUtils.isEmpty(edit_redpacket_number.getText().toString())
                        && Double.valueOf(edit_redpacket_number.getText().toString()) > 0) {
                    if (edit_redpacket_sum.getText().toString().equals("0.")) {
                        edit_redpacket_sum.setText("0");
                    }
                    Double sum = Double.valueOf(edit_redpacket_sum.getText().toString());
                    int number = Integer.parseInt(edit_redpacket_number.getText().toString());
                    DecimalFormat decimalFormat = new DecimalFormat("0.00");
                    String distanceString = decimalFormat.format(sum / number);
                    edit_redpacket_sum.setText(distanceString);
                } else {
                    edit_redpacket_sum.setText(edit_redpacket_sum.getText().toString());

                }
            } else {
                isPinRedPacket = true;
                img_pin.setVisibility(View.VISIBLE);
                tvRedpacketSum.setText("总金额");
                tv_redpacket_present.setText("当前为拼手气红包，改为");
                tv_redpacket_type.setText("普通红包");
                if (!TextUtils.isEmpty(edit_redpacket_sum.getText().toString()) && !TextUtils.isEmpty(edit_redpacket_number.getText().toString())
                        && Double.valueOf(edit_redpacket_number.getText().toString()) > 0) {
                    if (edit_redpacket_sum.getText().toString().equals("0.")) {
                        edit_redpacket_sum.setText("0");
                    }
                    Double sum = Double.valueOf(edit_redpacket_sum.getText().toString());
                    int number = Integer.parseInt(edit_redpacket_number.getText().toString());
                    DecimalFormat decimalFormat = new DecimalFormat("0.00");
                    String distanceString = decimalFormat.format(sum * number);
                    edit_redpacket_sum.setText(distanceString);
                } else {
                    edit_redpacket_sum.setText(edit_redpacket_sum.getText().toString());
                }
            }
        } else if (v == btnSendRedpacket) {
            RedPacketDialog dialog = new RedPacketDialog(this, new RedPacketDialog.OnSendRedPacketListener() {
                @Override
                public void senRedPacketClick() {
                    sendRedPacket();
                }
            });
            dialog.show();
            //发送红包
//            sendRedPacket();
        }
    }

    /**
     * @author: czl
     * @description 发送红包 调起支付宝支付
     * @date: 2019/10/9 16:32
     */
    private void sendRedPacket() {
        if (!DialogHelper.isShowing()) {
            DialogHelper.showDefaulteMessageProgressDialog(this);
        }
        String greetings = TextUtils.isEmpty(edit_input_blessing.getText().toString()) ? coreManager.readConfigBean().getDefaultGreetings() : edit_input_blessing.getText().toString();

        double sun = Double.valueOf(edit_redpacket_sum.getText().toString());
        int numble = Integer.parseInt(edit_redpacket_number.getText().toString());
        double money = isPinRedPacket ? sun : sun * numble;
        Map<String, String> params = new HashMap();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("type", isPinRedPacket ? "2" : "1");
        params.put("money", String.valueOf(money));
        params.put("count", String.valueOf(numble));
        params.put("greetings", greetings);
        params.put("roomId", roomId);

        HttpUtils.post().url(coreManager.getConfig().RED_PACKET_SEND)
                .params(params)
                .build()
                .execute(new BaseCallback<SendRedPacketBean>(SendRedPacketBean.class) {

                    @Override
                    public void onResponse(ObjectResult<SendRedPacketBean> result) {
                        DialogHelper.dismissProgressDialog();
                        if (result.getResultCode() == 1) {
                            mSendRedPacketBean = result.getData();
                            ChatMessage message = new ChatMessage();
                            message.setType(XmppMessage.TYPE_RED);
                            message.setFromUserId(mLoginUserId);
                            RoomMember member = RoomMemberDao.getInstance().getSingleRoomMember(mFriend.getRoomId(), mLoginUserId);
                            if (member != null && !TextUtils.isEmpty(member.getCardName())) {
                                message.setFromUserName(member.getCardName());
                            } else if (!TextUtils.isEmpty(mFriend.getRoomMyNickName())) {
                                message.setFromUserName(mFriend.getRoomMyNickName());
                            } else {
                                message.setFromUserName(mLoginNickName);
                            }
                            message.setContent(greetings); // 祝福语
                            message.setFileSize(0); // 用FilePath来储存红包类型
                            message.setMessageState(ChatMessageListener.MESSAGE_SEND_ING);
                            message.setMySend(true);// 表示是自己发的
                            message.setPacketId(mSendRedPacketBean.getMessageId());// 消息id
                            message.setObjectId(mSendRedPacketBean.getRedId());// objectid  存红包id
                            message.setTimeSend(TimeUtils.sk_time_current_time());
                            //本地保存一条红包消息
                            ChatMessageDao.getInstance().saveNewSingleChatMessage(mLoginUserId, toUserId, message);
                            AliPayHelper.pay(mSendRedPacketBean.getOrderInfo(), MucSendRedPacketActivity1.this, alipayResult);
                        } else {
                            ToastUtil.showToast(MucSendRedPacketActivity1.this, result.getResultMsg());
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        DialogHelper.
                                dismissProgressDialog();
                        ToastUtil.showToast(MucSendRedPacketActivity1.this, e.toString());
                    }
                });
    }
}
